package com.sunday.scoremanager.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;

import com.squareup.picasso.Picasso;
import com.sunday.common.utils.ImageUtils;
import com.sunday.scoremanager.R;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by 刘涛 on 2017/10/30.
 */

public class GridAdapter extends BaseAdapter {

    private View.OnClickListener onClickListener;
    private final int maxImg=9;
    private Context mContext;
    private List<String> imgList=new ArrayList<>();

    public GridAdapter(Context context,List<String> datas){
        this.mContext=context;
        this.imgList=datas;
    }

    @Override
    public int getCount() {
        int count=imgList.size()>=maxImg?maxImg:(imgList.size()+1);
        return count;
    }

    @Override
    public Object getItem(int position) {
        if (imgList.size()>=maxImg){
            return imgList.get(position);
        }else {
            if (position==imgList.size()){
                return "";
            }else return imgList.get(position);
        }
        //return imgList.size()>=maxImg?imgList.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {

        ViewHower viewHower;
        if (convertView == null) {
            viewHower = new ViewHower();
            convertView = LayoutInflater.from(mContext).inflate(R.layout.item_img, null);
            // viewHower.imageView = (ImageView) convertView.findViewById(R.id.img1);
            //viewHower.image = (ImageView) convertView.findViewById(R.id.img11);
            convertView.setTag(viewHower);
        } else {
            viewHower = (ViewHower) convertView.getTag();
        }

        viewHower.imageView = (ImageView) convertView.findViewById(R.id.img1);
        viewHower.image = (ImageView) convertView.findViewById(R.id.img11);
        if (position == imgList.size()) {
            viewHower.imageView.setImageResource(R.mipmap.add_product);
            viewHower.image.setVisibility(View.GONE);
        } else {
            Picasso.with(mContext)
                    .load(new File(ImageUtils.getCompressImagePath(imgList.get(position))))
                    .error(R.mipmap.ic_default)
                    .into(viewHower.imageView);
            viewHower.image.setVisibility(View.VISIBLE);
        }
        viewHower.imageView.setOnClickListener(onClickListener);
        viewHower.image.setOnClickListener(onClickListener);
        viewHower.imageView.setTag(position);
        viewHower.image.setTag(position);
        return convertView;
    }
    public void setOnClickListener(View.OnClickListener onClickListener) {
        this.onClickListener = onClickListener;
    }

    class ViewHower {
        ImageView imageView;
        ImageView image;
    }
}
